<template>
	<a-row :gutter="10">
		<a-col :xs="24" :sm="24" :md="24" :lg="5" :xl="5">
			<a-card :bordered="false" :loading="cardLoading" class="left-tree-container">
				<a-tree
					v-if="treeData.length > 0"
					v-model:expandedKeys="defaultExpandedKeys"
					:tree-data="treeData"
					:field-names="treeFieldNames"
					@select="treeSelect"
				/>
				<a-empty v-else :image="Empty.PRESENTED_IMAGE_SIMPLE" />
			</a-card>
		</a-col>
		<a-col :xs="24" :sm="24" :md="24" :lg="19" :xl="19">
			<a-card :bordered="false">
				<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
					<a-row :gutter="24">
						<a-col :span="6">
							<a-form-item label="试卷名称" name="paperName">
								<a-input v-model:value="searchFormState.paperName" placeholder="请输入试卷名称" />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="选题模式" name="selectionMode">
								<a-select
									v-model:value="searchFormState.selectionMode"
									placeholder="请选择选题模式"
									:options="selectionModeOptions"
								/>
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-form-item label="使用开始时间" name="startDate">
								<a-range-picker
									v-model:value="searchFormState.startDate"
									value-format="YYYY-MM-DD HH:mm:ss"
									show-time
								/>
							</a-form-item>
						</a-col>
						<a-col :span="6" v-show="advanced">
							<a-form-item label="使用结束时间" name="endDate">
								<a-range-picker v-model:value="searchFormState.endDate" show-time />
							</a-form-item>
						</a-col>
						<a-col :span="6" v-show="advanced">
							<a-form-item label="仅用于考场" name="onlyForExamHall">
								<a-select
									v-model:value="searchFormState.onlyForExamHall"
									placeholder="请选择仅用于考场"
									:options="onlyForExamHallOptions"
								/>
							</a-form-item>
						</a-col>
						<a-col :span="6" v-show="advanced">
							<a-form-item label="状态" name="status">
								<a-select v-model:value="searchFormState.status" placeholder="请选择状态" :options="statusOptions" />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
							<a-button style="margin: 0 8px" @click="reset">重置</a-button>
							<a @click="toggleAdvanced" style="margin-left: 8px">
								{{ advanced ? '收起' : '展开' }}
								<component :is="advanced ? 'up-outlined' : 'down-outlined'" />
							</a>
						</a-col>
					</a-row>
				</a-form>
				<s-table
					ref="tableRef"
					:columns="columns"
					:data="loadData"
					:alert="options.alert.show"
					bordered
					:row-key="(record) => record.id"
					:tool-config="toolConfig"
					:row-selection="options.rowSelection"
				>
					<template #operator class="table-operator">
						<a-space>
							<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('tblExamPaperAdd')">
								<template #icon><plus-outlined /></template>
								新增
							</a-button>
							<xn-batch-button
								v-if="hasPerm('tblExamPaperBatchDelete')"
								buttonName="批量删除"
								icon="DeleteOutlined"
								:selectedRowKeys="selectedRowKeys"
								@batchCallBack="deleteBatchTblExamPaper"
							/>
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
						<template v-if="column.dataIndex === 'selectionMode'">
							{{ $TOOL.dictTypeData('exam_paper_selection_mode', record.selectionMode) }}
						</template>
						<template v-if="column.dataIndex === 'onlyForExamHall'">
							{{ $TOOL.dictTypeData('yes_no', record.onlyForExamHall) }}
						</template>
						<template v-if="column.dataIndex === 'coverImage'">
							<a-image :src="record.coverImage" style="width: 30px; height: 30px" />
						</template>
						<template v-if="column.dataIndex === 'category'">
							{{ record.category.name }}
						</template>
						<template v-if="column.dataIndex === 'status'">
							{{ $TOOL.dictTypeData('status', record.status) }}
						</template>
						<template v-if="column.dataIndex === 'action'">
							<a-space>
								<a @click="formRef.onOpen(record)" v-if="hasPerm('tblExamPaperEdit')">编辑</a>
								<a-divider type="vertical" v-if="hasPerm(['tblExamPaperEdit', 'tblExamPaperDelete'], 'and')" />
								<a-popconfirm title="确定要删除吗？" @confirm="deleteTblExamPaper(record)">
									<a-button type="link" danger size="small" v-if="hasPerm('tblExamPaperDelete')">删除</a-button>
								</a-popconfirm>
							</a-space>
						</template>
					</template>
				</s-table>
			</a-card></a-col
		>
	</a-row>

	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="examPaper">
	import tool from '@/utils/tool'
	import { cloneDeep, isEmpty } from 'lodash-es'
	import Form from './form.vue'
	import tblExamPaperApi from '@/api/biz/tblExamPaperApi'
	import { Empty } from 'ant-design-vue'
	import tblCategoryApi from '@/api/biz/tblCategoryApi'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const treeData = ref([])
	const cardLoading = ref(true)
	const defaultExpandedKeys = ref([])
	const treeFieldNames = { children: 'children', title: 'name', key: 'id' }
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '试卷名称',
			dataIndex: 'paperName'
		},
		{
			title: '所属分类',
			dataIndex: 'category'
		},
		{
			title: '封面图片',
			dataIndex: 'coverImage'
		},
		{
			title: '题目数量',
			dataIndex: 'questionCount'
		},
		{
			title: '试卷总分',
			dataIndex: 'totalScore'
		},
		{
			title: '选题模式',
			dataIndex: 'selectionMode'
		},
		{
			title: '考试限时/分钟',
			dataIndex: 'examTimeLimit'
		},
		{
			title: '每日限制考试次数',
			dataIndex: 'dailyAttemptLimit'
		},
		{
			title: '使用开始时间',
			dataIndex: 'startDate'
		},
		{
			title: '使用结束时间',
			dataIndex: 'endDate'
		},
		{
			title: '仅用于考场',
			dataIndex: 'onlyForExamHall'
		},

		{
			title: '状态',
			dataIndex: 'status'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['tblExamPaperEdit', 'tblExamPaperDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		// startDate范围查询条件重载
		if (searchFormParam.startDate) {
			searchFormParam.startStartDate = searchFormParam.startDate[0]
			searchFormParam.endStartDate = searchFormParam.startDate[1]
			delete searchFormParam.startDate
		}
		// endDate范围查询条件重载
		if (searchFormParam.endDate) {
			searchFormParam.startEndDate = searchFormParam.endDate[0]
			searchFormParam.endEndDate = searchFormParam.endDate[1]
			delete searchFormParam.endDate
		}
		loadTreeData()
		return tblExamPaperApi.tblExamPaperPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteTblExamPaper = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		tblExamPaperApi.tblExamPaperDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchTblExamPaper = (params) => {
		tblExamPaperApi.tblExamPaperDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	// 加载左侧的树
	const loadTreeData = () => {
		tblCategoryApi.categoryTreeSelector({ type: 'paper' }).then((res) => {
			cardLoading.value = false
			if (res !== null) {
				treeData.value = res
				if (isEmpty(defaultExpandedKeys.value)) {
					// 默认展开2级
					treeData.value.forEach((item) => {
						// 因为0的顶级
						if (item.parentId === '0') {
							defaultExpandedKeys.value.push(item.id)
							// 取到下级ID
							if (item.children) {
								item.children.forEach((items) => {
									defaultExpandedKeys.value.push(items.id)
								})
							}
						}
					})
				}
			}
		})
	}
	// 点击树查询
	const treeSelect = (selectedKeys) => {
		if (selectedKeys.length > 0) {
			searchFormState.value.categoryId = selectedKeys.toString()
		} else {
			delete searchFormState.value.categoryId
		}
		tableRef.value.refresh(true)
	}
	const selectionModeOptions = tool.dictList('exam_paper_selection_mode')
	const onlyForExamHallOptions = tool.dictList('yes_no')
	const statusOptions = tool.dictList('status')
</script>
